rm(list = ls())
library(tidyverse)
library(broom)
library(haven)
library(polycor)
library(wCorr)
library(quanteda)
library(quanteda.textplots)
require(glmnet)
require(quanteda.textstats)
library(ggcorrplot)
library(tidymodels)
library(survey)

weightedCorr_na <- function(x,y,weights,...){
  
  x_missing <- which(is.na(x))
  y_missing <- which(is.na(y))
  missing <- unique(c(x_missing, y_missing))
  weights <- weights[-missing]
  
  weightedCorr(x[-missing], y[-missing], weights = weights,...)  
}
mae <- function(x, weights) weighted.mean(abs(x - mean(x, na.rm = T)), na.rm = TRUE, w = weights)

just_w1 <- read_sav("../data/new_survey/OP18397 UCL - Political research.sav")

just_w1_base <- just_w1

just_w2 <- read_sav("../data/new_survey/OP18397 UCL - Political research - WAVE 2.sav")

just_w2_base <- just_w2

just_w1 <- just_w1 %>%
  transmute(Id = as.character(AAltIdString.1),
            match_id = as.character(AlookupID.1),
            treat = as_factor(ATreat_Control),
            treat = factor(treat, levels = c("Control", "Treatment")),
            passed_attention_check_1 = ATrap_1 == 1,
            passed_attention_check_2 = ATrap_2 == 5,
            clean = clean,
            
            survey_date = as.Date(as.character(AEndDate.1), "%Y%m%d"),
            duration = as.numeric(ADuration.1),
            
            # Convert to numeric
            minimum_wage = as.numeric(Aminimum_wage),
            zero_hours = as.numeric(Azero_hours),
            high_tax = as.numeric(Ahigh_tax),
            unemployment_support = as.numeric(Aunemployment_support),
            trans_rights = as.numeric(Atrans_rights),
            offensive_speech = as.numeric(Aoffensive_speech),
            
            minimum_wage_text = as_factor(Aminimum_wage),
            zero_hours_text = as_factor(Azero_hours),
            high_tax_text = as_factor(Ahigh_tax),
            unemployment_support_text = as_factor(Aunemployment_support),
            trans_rights_text = as_factor(Atrans_rights),
            offensive_speech_text = as_factor(Aoffensive_speech),
            
            # Pair IDs
            
            saw_mw_zero = !is.na(minimum_wage) & !is.na(zero_hours),
            saw_tax_unemp = !is.na(high_tax) & !is.na(unemployment_support),
            saw_trans_offense = !is.na(trans_rights) & !is.na(offensive_speech),
            
            # Code DK responses
            
            dk_minimum_wage = minimum_wage == 6,
            dk_zero_hours = zero_hours == 6,
            dk_high_tax = high_tax == 6,
            dk_unemployment_support = unemployment_support == 6,
            dk_trans_rights = trans_rights == 6,
            dk_offensive_speech = offensive_speech == 6,
            
            # Remove DK responses
            minimum_wage = ifelse(minimum_wage == 6, NA, minimum_wage),
            zero_hours = ifelse(zero_hours == 6, NA, zero_hours),
            high_tax = ifelse(high_tax == 6, NA, high_tax),
            unemployment_support = ifelse(unemployment_support == 6, NA, unemployment_support),
            trans_rights = ifelse(trans_rights == 6, NA, trans_rights),
            offensive_speech = ifelse(offensive_speech == 6, NA, offensive_speech),
            
            # Recode so left-wing positions are larger numbers
            unemployment_support = 6 - unemployment_support,
            trans_rights = 6 - trans_rights,
            
            #offensive_speech = 6 - offensive_speech,
            
            just_minimum_wage = as.character(Aminimum_wage_prompt_treat.1),
            just_zero_hours = as.character(AZero_hours_prompts_treat.1),
            just_high_tax = as.character(AHigh_tax_prompt_treat.1),
            just_unemployment_support = as.character(Aunemployment_support_prompt_treat.1),
            just_trans_rights = as.character(Atrans_rights_prompts_treat.1),
            just_offensive_speech = as.character(AOffensive_Speech_prompts_treat.1),
            
            vote19 = as_factor(AV005ge19),
            vote16 = as_factor(AV005eu16),
            attention = as_factor(Apol_atten),
            attention = as.numeric(attention),
            
            attention_cat = case_when(attention %in% c(1:3) ~ "Low",
                                      attention %in% c(4:8,12) ~ "Medium",
                                      attention %in% c(9:11) ~ "High"),
            attention_cat = factor(attention_cat, levels = c("Low", "Medium", "High")),
            
            attention_cat2 = case_when(attention %in% c(1:8,12) ~ "Low",
                                      attention %in% c(9:11) ~ "High"),
            attention_cat2 = factor(attention_cat2, levels = c("Low", "High")),
            
            attention = ifelse(attention == 12, NA, attention),
            above_med_attention = attention >=8,
            
            ## Duration
            
            duration_tax_text = ADuration_tax_promt.1 - AStart_tax_promt.1,
            duration_tax_opinion = ADuration_tax_high.1 - AStart_tax_high.1,
            
            duration_unemp_text = ADuration_unemployment_promt.1 - AStart_unemployment_prompt.1,
            duration_unemp_opinion = ADuration_unemployment.1 - AStart_unemployment.1,
            
            duration_mw_text = ADuration_wage_promt.1 - AStart_wage_prompt.1,
            duration_mw_opinion = ADuration_wage.1 - AStart_wage.1,
            
            duration_zero_text = ADuration_zero_promt.1 - AStart_zero_prompt.1,
            duration_zero_opinion = ADuration_zero.1 - AStart_zero.1,
            
            duration_speech_text = ADuration_speach_promt.1 - AStart_speach_prompt.1,
            duration_speech_opinion = ADuration_speach.1 - AStart_speach.1,
            
            duration_trans_text = ADuration_trans_promt.1 - AStart_trans_prompt.1,
            duration_trans_opinion = ADuration_trans.1 - AStart_trans.1,
            
            duration_tax_text = ifelse(duration_tax_text > 0, duration_tax_text, 0),
            duration_tax_opinion = ifelse(duration_tax_opinion > 0, duration_tax_opinion, 0),
            duration_unemp_text = ifelse(duration_unemp_text > 0, duration_unemp_text, 0),
            duration_unemp_opinion = ifelse(duration_unemp_opinion > 0, duration_unemp_opinion, 0),
            duration_mw_text = ifelse(duration_mw_text > 0, duration_mw_text, 0),
            duration_mw_opinion = ifelse(duration_mw_opinion > 0, duration_mw_opinion, 0),
            duration_zero_text = ifelse(duration_zero_text > 0, duration_zero_text, 0),
            duration_zero_opinion = ifelse(duration_zero_opinion > 0, duration_zero_opinion, 0),
            duration_speech_text = ifelse(duration_speech_text > 0, duration_speech_text, 0),
            duration_speech_opinion = ifelse(duration_speech_opinion > 0, duration_speech_opinion, 0),
            duration_trans_text = ifelse(duration_trans_text > 0, duration_trans_text, 0),
            duration_trans_opinion = ifelse(duration_trans_opinion > 0, duration_trans_opinion, 0),
            
            
            gender = ifelse(CD1_Gender==1, "Male", "Female"),
            age = as.numeric(CD2_Age),
            med_income = APERSONAL_INCOME,
            above_med_income = ifelse(APERSONAL_INCOME < 12, APERSONAL_INCOME, NA) > 2,
            party_simple = case_when(vote19 == "Conservatives" ~ "Conservatives",
                                     vote19 == "Labour" ~ "Labour",
                                     is.na(vote19) ~ NA_character_,
                                     !vote19 %in% c("Labour", "Conservatives") ~ "Other"),
            home_owner = ifelse(ATenure %in% c(1,2),TRUE,FALSE),
            eu_vote = ifelse(grepl("Leave",as.character(as_factor(AV005eu16))), "Leave", "Remain"),
            eu_vote = ifelse(is.na(AV005eu16), "Other", eu_vote),
            eu_vote = factor(eu_vote, levels = c("Leave", "Remain", "Other")),
            social_grade = case_when(AD5_socialgrade %in% c(1,2,3,6) ~ "Working",
                                     AD5_socialgrade %in% c(4,5,7,8) ~ "Not Working"),
            age_cat = case_when(age <= 34 ~ "18-34",
                                age > 34 & age <= 54 ~ "35-54",
                                age > 54 ~ "55+"),
            employment = case_when(AD3_employment == 1 ~ "full_time",
                                   AD3_employment %in% c(2,3) ~ "part_time",
                                   AD3_employment %in% c(4,7) ~ "student_other",
                                   AD3_employment %in% c(5) ~ "retired",
                                   AD3_employment %in% c(6) ~ "unemployed"),
            
            education = case_when(AD_education_1 %in% c(1:5) ~ "Below degree level",
                                  AD_education_1 %in% c(6:11) ~ "Degree level and above"),
            
            
            region = as_factor(CD4_Region),
            vote19_quota = case_when(AV005ge19 == 1 ~ "Conservative",
                                     AV005ge19 == 2 ~ "Labour",
                                     AV005ge19 == 3 ~ "Liberal Democrats",
                                     AV005ge19 == 4 ~ "Scottish National Party",
                                     AV005ge19 == 5 ~ "Plaid Cymru",
                                     AV005ge19 == 6 ~ "Brexit Party",
                                     AV005ge19 == 8 ~ "Green",
                                     AV005ge19 %in% c(7,8,9) ~ "Other",
                                     is.na(AV005ge19) ~ "Not sure"),
            vote19_quota = factor(vote19_quota, levels = c("Conservative", "Labour", "Liberal Democrats", "Scottish National Party", "Plaid Cymru", "Brexit Party", "Green", "Other", "Not sure")),
            vote19_simple = case_when(AV005ge19 == 1 ~ "Conservative",
                                      AV005ge19 == 2 ~ "Labour",
                                      AV005ge19 %in% c(3:9) ~ "Other",
                                      is.na(AV005ge19) ~ "Other"),
            vote19_simple = factor(vote19_simple, levels = c("Conservative", "Labour", "Other"))
            )

just_w2 <- just_w2 %>% 
  mutate(ATrap_1 = ifelse(is.na(ATrap_1), 1, ATrap_1),
         ATrap_2 = ifelse(is.na(ATrap_2), 5, ATrap_2)) %>%
     transmute(Id = as.character(Aaltid.1),
            match_id = as.character(AlookupID.1),
            
            survey_date_w2 = as.Date(as.character(AEndDate.1), "%Y%m%d"),
            
            # Wave 1 treatment status for new observations
            treat = as_factor(ATreat_Control),
            treat = factor(treat, levels = c("Control", "Treatment")),
            
            # Wave 2 treatment status
            treat_w2 = as_factor(ATreat_Control_W2),
            treat_w2 = factor(treat_w2, levels = c("Control", "Treatment")),
            
            passed_attention_check_1 = ATrap_1 == 1,
            passed_attention_check_2 = ATrap_2 == 5,
            
            ATreat_Control = as_factor(ATreat_Control),
            ATreat_Control_W2 = as_factor(ATreat_Control_W2),
            BTreat_Control = as_factor(BTreat_Control),
            CTreat_Control = as_factor(CTreat_Control),
            
            # Convert to numeric
            minimum_wage_w2 = as.numeric(Aminimum_wage),
            zero_hours_w2 = as.numeric(Azero_hours),
            high_tax_w2 = as.numeric(Ahigh_tax),
            unemployment_support_w2 = as.numeric(Aunemployment_support),
            trans_rights_w2 = as.numeric(Atrans_rights),
            offensive_speech_w2 = as.numeric(Aoffensive_speech),
            
            minimum_wage_text_w2 = as_factor(Aminimum_wage_prompt_treat.1),
            zero_hours_text_w2 = as_factor(AZero_hours_prompts_treat.1),
            high_tax_text_w2 = as_factor(AHigh_tax_prompt_treat.1),
            unemployment_support_text_w2 = as_factor(Aunemployment_support_prompt_treat.1),
            trans_rights_text_w2 = as_factor(Atrans_rights_prompts_treat.1),
            offensive_speech_text_w2 = as_factor(AOffensive_Speech_prompts_treat.1),
            
            # Pair IDs
            
            saw_mw_zero_w2 = !is.na(minimum_wage_w2) & !is.na(zero_hours_w2),
            saw_tax_unemp_w2 = !is.na(high_tax_w2) & !is.na(unemployment_support_w2),
            saw_trans_offense_w2 = !is.na(trans_rights_w2) & !is.na(offensive_speech_w2),
            
            # Code DK responses
            
            dk_minimum_wage_w2 = minimum_wage_w2 == 6,
            dk_zero_hours_w2 = zero_hours_w2 == 6,
            dk_high_tax_w2 = high_tax_w2 == 6,
            dk_unemployment_support_w2 = unemployment_support_w2 == 6,
            dk_trans_rights_w2 = trans_rights_w2 == 6,
            dk_offensive_speech_w2 = offensive_speech_w2 == 6,
            
            # Remove DK responses
            minimum_wage_w2 = ifelse(minimum_wage_w2 == 6, NA, minimum_wage_w2),
            zero_hours_w2 = ifelse(zero_hours_w2 == 6, NA, zero_hours_w2),
            high_tax_w2 = ifelse(high_tax_w2 == 6, NA, high_tax_w2),
            unemployment_support_w2 = ifelse(unemployment_support_w2 == 6, NA, unemployment_support_w2),
            trans_rights_w2 = ifelse(trans_rights_w2 == 6, NA, trans_rights_w2),
            offensive_speech_w2 = ifelse(offensive_speech_w2 == 6, NA, offensive_speech_w2),
            
            # Recode so left-wing positions are larger numbers
            unemployment_support_w2 = 6 - unemployment_support_w2,
            trans_rights_w2 = 6 - trans_rights_w2,
            #offensive_speech = 6 - offensive_speech,
            
            just_minimum_wage_w2 = as.character(Aminimum_wage_prompt_treat.1),
            just_zero_hours_w2 = as.character(AZero_hours_prompts_treat.1),
            just_high_tax_w2 = as.character(AHigh_tax_prompt_treat.1),
            just_unemployment_support_w2 = as.character(Aunemployment_support_prompt_treat.1),
            just_trans_rights_w2 = as.character(Atrans_rights_prompts_treat.1),
            just_offensive_speech_w2 = as.character(AOffensive_Speech_prompts_treat.1),
            
            ## Which issues did respondents see in experiment 2?
            
            saw_zero_w2 = ifelse(!is.na(APairs_W2_1),1,0),
            saw_high_tax_w2 = ifelse(!is.na(APairs_W2_2),1,0),
            saw_mw_w2 = ifelse(!is.na(APairs_W2_3),1,0),
            saw_unemp_w2 = ifelse(!is.na(APairs_W2_4),1,0),
            saw_trans_w2 = ifelse(!is.na(APairs_W2_5),1,0),
            saw_offense_w2 = ifelse(!is.na(APairs_W2_6),1,0),
            
            ## Binary issue position on each issue
            
            binary_zero = case_when(Apolicy_against_1 == 1 ~ "Against",
                                    Apolicy_infavour_1 == 1 ~ "In favour"),
            binary_high_tax = case_when(Apolicy_against_2 == 1 ~ "Against",
                                    Apolicy_infavour_2 == 1 ~ "In favour"),
            binary_mw = case_when(Apolicy_against_3 == 1 ~ "Against",
                                        Apolicy_infavour_3 == 1 ~ "In favour"),
            binary_unemployment = case_when(Apolicy_against_4 == 1 ~ "Against",
                                  Apolicy_infavour_4 == 1 ~ "In favour"),
            binary_trans = case_when(Apolicy_against_5 == 1 ~ "Against",
                                            Apolicy_infavour_5 == 1 ~ "In favour"),
            binary_offense = case_when(Apolicy_against_6 == 1 ~ "Against",
                                     Apolicy_infavour_6 == 1 ~ "In favour"),
            
            ## Favourability
            
            therm_favour_zero = as.numeric(AfeelingN1),
            therm_against_zero = as.numeric(AfeelingN2),
            
            therm_favour_tax = as.numeric(AfeelingN3),
            therm_against_tax = as.numeric(AfeelingN4),
            
            therm_favour_wage = as.numeric(AfeelingN5),
            therm_against_wage = as.numeric(AfeelingN6),
            
            therm_favour_unemploy = as.numeric(AfeelingN7),
            therm_against_unemploy = as.numeric(AfeelingN8),
            
            therm_favour_trans = as.numeric(AfeelingN9),
            therm_against_trans = as.numeric(AfeelingN10),
            
            therm_favour_off = as.numeric(AfeelingN11),
            therm_against_off = as.numeric(AfeelingN12),
            
            therm_ingroup_zero = ifelse(binary_zero == "In favour", therm_favour_zero, therm_against_zero),
            therm_outgroup_zero = ifelse(binary_zero == "Against", therm_favour_zero, therm_against_zero),
            therm_diff_zero = therm_ingroup_zero - therm_outgroup_zero,
            
            therm_ingroup_tax = ifelse(binary_high_tax == "In favour", therm_favour_tax, therm_against_tax),
            therm_outgroup_tax = ifelse(binary_high_tax == "Against", therm_favour_tax, therm_against_tax),
            therm_diff_tax = therm_ingroup_tax - therm_outgroup_tax,
            
            therm_ingroup_wage = ifelse(binary_mw == "In favour", therm_favour_wage, therm_against_wage),
            therm_outgroup_wage = ifelse(binary_mw == "Against", therm_favour_wage, therm_against_wage),
            therm_diff_wage = therm_ingroup_wage - therm_outgroup_wage,
            
            therm_ingroup_unemploy = ifelse(binary_unemployment == "In favour", therm_favour_unemploy, therm_against_unemploy),
            therm_outgroup_unemploy = ifelse(binary_unemployment == "Against", therm_favour_unemploy, therm_against_unemploy),
            therm_diff_unemploy = therm_ingroup_unemploy - therm_outgroup_unemploy,
            
            therm_ingroup_trans = ifelse(binary_trans == "In favour", therm_favour_trans, therm_against_trans),
            therm_outgroup_trans = ifelse(binary_trans == "Against", therm_favour_trans, therm_against_trans),
            therm_diff_trans = therm_ingroup_trans - therm_outgroup_trans,
            
            therm_ingroup_off = ifelse(binary_offense == "In favour", therm_favour_off, therm_against_off),
            therm_outgroup_off = ifelse(binary_offense == "Against", therm_favour_off, therm_against_off),
            therm_diff_off = therm_ingroup_off - therm_outgroup_off,
            
            ## Trait items -- CHECK THE CODING OF THESE
            
            trait_zero_favour_intelligent = as.numeric(Azero1_1N1),
            trait_zero_favour_open = as.numeric(Azero1_1N2),
            trait_zero_favour_honest = as.numeric(Azero1_1N3),
            trait_zero_favour_selfish = as.numeric(Azero1_1N4),
            trait_zero_favour_hypocritical = as.numeric(Azero1_1N5),
            trait_zero_favour_closed = as.numeric(Azero1_1N6),
            
            trait_zero_against_intelligent = as.numeric(Azero1_2N1),
            trait_zero_against_open = as.numeric(Azero1_2N2),
            trait_zero_against_honest = as.numeric(Azero1_2N3),
            trait_zero_against_selfish = as.numeric(Azero1_2N4),
            trait_zero_against_hypocritical = as.numeric(Azero1_2N5),
            trait_zero_against_closed = as.numeric(Azero1_2N6),
            
            trait_tax_favour_intelligent = as.numeric(Atax1_1N1),
            trait_tax_favour_open = as.numeric(Atax1_1N2),
            trait_tax_favour_honest = as.numeric(Atax1_1N3),
            trait_tax_favour_selfish = as.numeric(Atax1_1N4),
            trait_tax_favour_hypocritical = as.numeric(Atax1_1N5),
            trait_tax_favour_closed = as.numeric(Atax1_1N6),
            
            trait_tax_against_intelligent = as.numeric(Atax1_2N1),
            trait_tax_against_open = as.numeric(Atax1_2N2),
            trait_tax_against_honest = as.numeric(Atax1_2N3),
            trait_tax_against_selfish = as.numeric(Atax1_2N4),
            trait_tax_against_hypocritical = as.numeric(Atax1_2N5),
            trait_tax_against_closed = as.numeric(Atax1_2N6),
            
            trait_wage_favour_intelligent = as.numeric(Awage1_1N1),
            trait_wage_favour_open = as.numeric(Awage1_1N2),
            trait_wage_favour_honest = as.numeric(Awage1_1N3),
            trait_wage_favour_selfish = as.numeric(Awage1_1N4),
            trait_wage_favour_hypocritical = as.numeric(Awage1_1N5),
            trait_wage_favour_closed = as.numeric(Awage1_1N6),
            
            trait_wage_against_intelligent = as.numeric(Awage1_2N1),
            trait_wage_against_open = as.numeric(Awage1_2N2),
            trait_wage_against_honest = as.numeric(Awage1_2N3),
            trait_wage_against_selfish = as.numeric(Awage1_2N4),
            trait_wage_against_hypocritical = as.numeric(Awage1_2N5),
            trait_wage_against_closed = as.numeric(Awage1_2N6),
            
            trait_unemploy_favour_intelligent = as.numeric(Aunemploy1_1N1),
            trait_unemploy_favour_open = as.numeric(Aunemploy1_1N2),
            trait_unemploy_favour_honest = as.numeric(Aunemploy1_1N3),
            trait_unemploy_favour_selfish = as.numeric(Aunemploy1_1N4),
            trait_unemploy_favour_hypocritical = as.numeric(Aunemploy1_1N5),
            trait_unemploy_favour_closed = as.numeric(Aunemploy1_1N6),
            
            trait_unemploy_against_intelligent = as.numeric(Aunemploy1_2N1),
            trait_unemploy_against_open = as.numeric(Aunemploy1_2N2),
            trait_unemploy_against_honest = as.numeric(Aunemploy1_2N3),
            trait_unemploy_against_selfish = as.numeric(Aunemploy1_2N4),
            trait_unemploy_against_hypocritical = as.numeric(Aunemploy1_2N5),
            trait_unemploy_against_closed = as.numeric(Aunemploy1_2N6),
            
            trait_trans_favour_intelligent = as.numeric(Atrans1_1N1),
            trait_trans_favour_open = as.numeric(Atrans1_1N2),
            trait_trans_favour_honest = as.numeric(Atrans1_1N3),
            trait_trans_favour_selfish = as.numeric(Atrans1_1N4),
            trait_trans_favour_hypocritical = as.numeric(Atrans1_1N5),
            trait_trans_favour_closed = as.numeric(Atrans1_1N6),
            
            trait_trans_against_intelligent = as.numeric(Atrans1_2N1),
            trait_trans_against_open = as.numeric(Atrans1_2N2),
            trait_trans_against_honest = as.numeric(Atrans1_2N3),
            trait_trans_against_selfish = as.numeric(Atrans1_2N4),
            trait_trans_against_hypocritical = as.numeric(Atrans1_2N5),
            trait_trans_against_closed = as.numeric(Atrans1_2N6),
            
            trait_off_favour_intelligent = as.numeric(Aoff1_1N1),
            trait_off_favour_open = as.numeric(Aoff1_1N2),
            trait_off_favour_honest = as.numeric(Aoff1_1N3),
            trait_off_favour_selfish = as.numeric(Aoff1_1N4),
            trait_off_favour_hypocritical = as.numeric(Aoff1_1N5),
            trait_off_favour_closed = as.numeric(Aoff1_1N6),
            
            trait_off_against_intelligent = as.numeric(Aoff1_2N1),
            trait_off_against_open = as.numeric(Aoff1_2N2),
            trait_off_against_honest = as.numeric(Aoff1_2N3),
            trait_off_against_selfish = as.numeric(Aoff1_2N4),
            trait_off_against_hypocritical = as.numeric(Aoff1_2N5),
            trait_off_against_closed = as.numeric(Aoff1_2N6),
            
            ## Trait same versus opposite
            
            # Zero
            
            trait_zero_same_intelligent = case_when(binary_zero == "In favour" ~ trait_zero_favour_intelligent,
                                                    binary_zero == "Against" ~ trait_zero_against_intelligent),
            trait_zero_same_open = case_when(binary_zero == "In favour" ~ trait_zero_favour_open,
                                                    binary_zero == "Against" ~ trait_zero_against_open),
            trait_zero_same_honest = case_when(binary_zero == "In favour" ~ trait_zero_favour_honest,
                                             binary_zero == "Against" ~ trait_zero_against_honest),
            trait_zero_same_selfish = case_when(binary_zero == "In favour" ~ trait_zero_favour_selfish,
                                             binary_zero == "Against" ~ trait_zero_against_selfish),
            trait_zero_same_hypocritical = case_when(binary_zero == "In favour" ~ trait_zero_favour_hypocritical,
                                             binary_zero == "Against" ~ trait_zero_against_hypocritical),
            trait_zero_same_closed = case_when(binary_zero == "In favour" ~ trait_zero_favour_closed,
                                             binary_zero == "Against" ~ trait_zero_against_closed),
            
            trait_zero_opposite_intelligent = case_when(binary_zero != "In favour" ~ trait_zero_favour_intelligent,
                                                        binary_zero != "Against" ~ trait_zero_against_intelligent),
            trait_zero_opposite_open = case_when(binary_zero != "In favour" ~ trait_zero_favour_open,
                                                 binary_zero != "Against" ~ trait_zero_against_open),
            trait_zero_opposite_honest = case_when(binary_zero != "In favour" ~ trait_zero_favour_honest,
                                                   binary_zero != "Against" ~ trait_zero_against_honest),
            trait_zero_opposite_selfish = case_when(binary_zero != "In favour" ~ trait_zero_favour_selfish,
                                                    binary_zero != "Against" ~ trait_zero_against_selfish),
            trait_zero_opposite_hypocritical = case_when(binary_zero != "In favour" ~ trait_zero_favour_hypocritical,
                                                         binary_zero != "Against" ~ trait_zero_against_hypocritical),
            trait_zero_opposite_closed = case_when(binary_zero != "In favour" ~ trait_zero_favour_closed,
                                                   binary_zero != "Against" ~ trait_zero_against_closed),
            
            trait_zero_scale_same = trait_zero_same_intelligent + trait_zero_same_open + trait_zero_same_honest + (6 - trait_zero_same_selfish) + (6 - trait_zero_same_hypocritical) + (6 - trait_zero_same_closed),
            
            trait_zero_scale_same = trait_zero_scale_same/6,
            
            trait_zero_scale_opposite = trait_zero_opposite_intelligent + trait_zero_opposite_open + trait_zero_opposite_honest + (6 - trait_zero_opposite_selfish) + (6 - trait_zero_opposite_hypocritical) + (6 - trait_zero_opposite_closed),
            
            trait_zero_scale_opposite = trait_zero_scale_opposite/6,
            
            trait_zero_scale = trait_zero_scale_same - trait_zero_scale_opposite,
            
            # Tax
            
            trait_tax_same_intelligent = case_when(binary_high_tax == "In favour" ~ trait_tax_favour_intelligent,
                                                   binary_high_tax == "Against" ~ trait_tax_against_intelligent),
            trait_tax_same_open = case_when(binary_high_tax == "In favour" ~ trait_tax_favour_open,
                                            binary_high_tax == "Against" ~ trait_tax_against_open),
            trait_tax_same_honest = case_when(binary_high_tax == "In favour" ~ trait_tax_favour_honest,
                                              binary_high_tax == "Against" ~ trait_tax_against_honest),
            trait_tax_same_selfish = case_when(binary_high_tax == "In favour" ~ trait_tax_favour_selfish,
                                               binary_high_tax == "Against" ~ trait_tax_against_selfish),
            trait_tax_same_hypocritical = case_when(binary_high_tax == "In favour" ~ trait_tax_favour_hypocritical,
                                                    binary_high_tax == "Against" ~ trait_tax_against_hypocritical),
            trait_tax_same_closed = case_when(binary_high_tax == "In favour" ~ trait_tax_favour_closed,
                                              binary_high_tax == "Against" ~ trait_tax_against_closed),
            
            trait_tax_opposite_intelligent = case_when(binary_high_tax != "In favour" ~ trait_tax_favour_intelligent,
                                                       binary_high_tax != "Against" ~ trait_tax_against_intelligent),
            trait_tax_opposite_open = case_when(binary_high_tax != "In favour" ~ trait_tax_favour_open,
                                                binary_high_tax != "Against" ~ trait_tax_against_open),
            trait_tax_opposite_honest = case_when(binary_high_tax != "In favour" ~ trait_tax_favour_honest,
                                                  binary_high_tax != "Against" ~ trait_tax_against_honest),
            trait_tax_opposite_selfish = case_when(binary_high_tax != "In favour" ~ trait_tax_favour_selfish,
                                                   binary_high_tax != "Against" ~ trait_tax_against_selfish),
            trait_tax_opposite_hypocritical = case_when(binary_high_tax != "In favour" ~ trait_tax_favour_hypocritical,
                                                        binary_high_tax != "Against" ~ trait_tax_against_hypocritical),
            trait_tax_opposite_closed = case_when(binary_high_tax != "In favour" ~ trait_tax_favour_closed,
                                                  binary_high_tax != "Against" ~ trait_tax_against_closed),
            
            trait_tax_scale_same = trait_tax_same_intelligent + trait_tax_same_open + trait_tax_same_honest + (6 - trait_tax_same_selfish) + (6 - trait_tax_same_hypocritical) + (6 - trait_tax_same_closed),
            
            trait_tax_scale_same = trait_tax_scale_same/6,
            
            trait_tax_scale_opposite = trait_tax_opposite_intelligent + trait_tax_opposite_open + trait_tax_opposite_honest + (6 - trait_tax_opposite_selfish) + (6 - trait_tax_opposite_hypocritical) + (6 - trait_tax_opposite_closed),
            
            trait_tax_scale_opposite = trait_tax_scale_opposite/6,
            
            trait_tax_scale = trait_tax_scale_same - trait_tax_scale_opposite,
            
            # Wage
            
            trait_wage_same_intelligent = case_when(binary_mw == "In favour" ~ trait_wage_favour_intelligent,
                                                    binary_mw == "Against" ~ trait_wage_against_intelligent),
            trait_wage_same_open = case_when(binary_mw == "In favour" ~ trait_wage_favour_open,
                                             binary_mw == "Against" ~ trait_wage_against_open),
            trait_wage_same_honest = case_when(binary_mw == "In favour" ~ trait_wage_favour_honest,
                                               binary_mw == "Against" ~ trait_wage_against_honest),
            trait_wage_same_selfish = case_when(binary_mw == "In favour" ~ trait_wage_favour_selfish,
                                                binary_mw == "Against" ~ trait_wage_against_selfish),
            trait_wage_same_hypocritical = case_when(binary_mw == "In favour" ~ trait_wage_favour_hypocritical,
                                                     binary_mw == "Against" ~ trait_wage_against_hypocritical),
            trait_wage_same_closed = case_when(binary_mw == "In favour" ~ trait_wage_favour_closed,
                                               binary_mw == "Against" ~ trait_wage_against_closed),
            
            trait_wage_opposite_intelligent = case_when(binary_mw != "In favour" ~ trait_wage_favour_intelligent,
                                                        binary_mw != "Against" ~ trait_wage_against_intelligent),
            trait_wage_opposite_open = case_when(binary_mw != "In favour" ~ trait_wage_favour_open,
                                                 binary_mw != "Against" ~ trait_wage_against_open),
            trait_wage_opposite_honest = case_when(binary_mw != "In favour" ~ trait_wage_favour_honest,
                                                   binary_mw != "Against" ~ trait_wage_against_honest),
            trait_wage_opposite_selfish = case_when(binary_mw != "In favour" ~ trait_wage_favour_selfish,
                                                    binary_mw != "Against" ~ trait_wage_against_selfish),
            trait_wage_opposite_hypocritical = case_when(binary_mw != "In favour" ~ trait_wage_favour_hypocritical,
                                                         binary_mw != "Against" ~ trait_wage_against_hypocritical),
            trait_wage_opposite_closed = case_when(binary_mw != "In favour" ~ trait_wage_favour_closed,
                                                   binary_mw != "Against" ~ trait_wage_against_closed),
            
            trait_wage_scale_same = trait_wage_same_intelligent + trait_wage_same_open + trait_wage_same_honest + (6 - trait_wage_same_selfish) + (6 - trait_wage_same_hypocritical) + (6 - trait_wage_same_closed),
            
            trait_wage_scale_same = trait_wage_scale_same/6,
            
            trait_wage_scale_opposite = trait_wage_opposite_intelligent + trait_wage_opposite_open + trait_wage_opposite_honest + (6 - trait_wage_opposite_selfish) + (6 - trait_wage_opposite_hypocritical) + (6 - trait_wage_opposite_closed),
            
            trait_wage_scale_opposite = trait_wage_scale_opposite/6,
            
            trait_wage_scale = trait_wage_scale_same - trait_wage_scale_opposite,
            
            # Unemploy
            
            trait_unemploy_same_intelligent = case_when(binary_unemployment == "In favour" ~ trait_unemploy_favour_intelligent,
                                                        binary_unemployment == "Against" ~ trait_unemploy_against_intelligent),
            trait_unemploy_same_open = case_when(binary_unemployment == "In favour" ~ trait_unemploy_favour_open,
                                                 binary_unemployment == "Against" ~ trait_unemploy_against_open),
            trait_unemploy_same_honest = case_when(binary_unemployment == "In favour" ~ trait_unemploy_favour_honest,
                                                   binary_unemployment == "Against" ~ trait_unemploy_against_honest),
            trait_unemploy_same_selfish = case_when(binary_unemployment == "In favour" ~ trait_unemploy_favour_selfish,
                                                    binary_unemployment == "Against" ~ trait_unemploy_against_selfish),
            trait_unemploy_same_hypocritical = case_when(binary_unemployment == "In favour" ~ trait_unemploy_favour_hypocritical,
                                                         binary_unemployment == "Against" ~ trait_unemploy_against_hypocritical),
            trait_unemploy_same_closed = case_when(binary_unemployment == "In favour" ~ trait_unemploy_favour_closed,
                                                   binary_unemployment == "Against" ~ trait_unemploy_against_closed),
            
            trait_unemploy_opposite_intelligent = case_when(binary_unemployment != "In favour" ~ trait_unemploy_favour_intelligent,
                                                            binary_unemployment != "Against" ~ trait_unemploy_against_intelligent),
            trait_unemploy_opposite_open = case_when(binary_unemployment != "In favour" ~ trait_unemploy_favour_open,
                                                     binary_unemployment != "Against" ~ trait_unemploy_against_open),
            trait_unemploy_opposite_honest = case_when(binary_unemployment != "In favour" ~ trait_unemploy_favour_honest,
                                                       binary_unemployment != "Against" ~ trait_unemploy_against_honest),
            trait_unemploy_opposite_selfish = case_when(binary_unemployment != "In favour" ~ trait_unemploy_favour_selfish,
                                                        binary_unemployment != "Against" ~ trait_unemploy_against_selfish),
            trait_unemploy_opposite_hypocritical = case_when(binary_unemployment != "In favour" ~ trait_unemploy_favour_hypocritical,
                                                             binary_unemployment != "Against" ~ trait_unemploy_against_hypocritical),
            trait_unemploy_opposite_closed = case_when(binary_unemployment != "In favour" ~ trait_unemploy_favour_closed,
                                                       binary_unemployment != "Against" ~ trait_unemploy_against_closed),
            
            trait_unemploy_scale_same = trait_unemploy_same_intelligent + trait_unemploy_same_open + trait_unemploy_same_honest + (6 - trait_unemploy_same_selfish) + (6 - trait_unemploy_same_hypocritical) + (6 - trait_unemploy_same_closed),
            
            trait_unemploy_scale_same = trait_unemploy_scale_same/6,
            
            trait_unemploy_scale_opposite = trait_unemploy_opposite_intelligent + trait_unemploy_opposite_open + trait_unemploy_opposite_honest + (6 - trait_unemploy_opposite_selfish) + (6 - trait_unemploy_opposite_hypocritical) + (6 - trait_unemploy_opposite_closed),
            
            trait_unemploy_scale_opposite = trait_unemploy_scale_opposite/6,
            
            trait_unemploy_scale = trait_unemploy_scale_same - trait_unemploy_scale_opposite,
            
            # Trans
            
            trait_trans_same_intelligent = case_when(binary_trans == "In favour" ~ trait_trans_favour_intelligent,
                                                     binary_trans == "Against" ~ trait_trans_against_intelligent),
            trait_trans_same_open = case_when(binary_trans == "In favour" ~ trait_trans_favour_open,
                                              binary_trans == "Against" ~ trait_trans_against_open),
            trait_trans_same_honest = case_when(binary_trans == "In favour" ~ trait_trans_favour_honest,
                                                binary_trans == "Against" ~ trait_trans_against_honest),
            trait_trans_same_selfish = case_when(binary_trans == "In favour" ~ trait_trans_favour_selfish,
                                                 binary_trans == "Against" ~ trait_trans_against_selfish),
            trait_trans_same_hypocritical = case_when(binary_trans == "In favour" ~ trait_trans_favour_hypocritical,
                                                      binary_trans == "Against" ~ trait_trans_against_hypocritical),
            trait_trans_same_closed = case_when(binary_trans == "In favour" ~ trait_trans_favour_closed,
                                                binary_trans == "Against" ~ trait_trans_against_closed),
            
            trait_trans_opposite_intelligent = case_when(binary_trans != "In favour" ~ trait_trans_favour_intelligent,
                                                         binary_trans != "Against" ~ trait_trans_against_intelligent),
            trait_trans_opposite_open = case_when(binary_trans != "In favour" ~ trait_trans_favour_open,
                                                  binary_trans != "Against" ~ trait_trans_against_open),
            trait_trans_opposite_honest = case_when(binary_trans != "In favour" ~ trait_trans_favour_honest,
                                                    binary_trans != "Against" ~ trait_trans_against_honest),
            trait_trans_opposite_selfish = case_when(binary_trans != "In favour" ~ trait_trans_favour_selfish,
                                                     binary_trans != "Against" ~ trait_trans_against_selfish),
            trait_trans_opposite_hypocritical = case_when(binary_trans != "In favour" ~ trait_trans_favour_hypocritical,
                                                          binary_trans != "Against" ~ trait_trans_against_hypocritical),
            trait_trans_opposite_closed = case_when(binary_trans != "In favour" ~ trait_trans_favour_closed,
                                                    binary_trans != "Against" ~ trait_trans_against_closed),
            
            trait_trans_scale_same = trait_trans_same_intelligent + trait_trans_same_open + trait_trans_same_honest + (6 - trait_trans_same_selfish) + (6 - trait_trans_same_hypocritical) + (6 - trait_trans_same_closed),
            
            trait_trans_scale_same = trait_trans_scale_same/6,
            
            trait_trans_scale_opposite = trait_trans_opposite_intelligent + trait_trans_opposite_open + trait_trans_opposite_honest + (6 - trait_trans_opposite_selfish) + (6 - trait_trans_opposite_hypocritical) + (6 - trait_trans_opposite_closed),
            
            trait_trans_scale_opposite = trait_trans_scale_opposite/6,
            
            trait_trans_scale = trait_trans_scale_same - trait_trans_scale_opposite,
            
            # Offensive speech
            
            trait_off_same_intelligent = case_when(binary_offense == "In favour" ~ trait_off_favour_intelligent,
                                                   binary_offense == "Against" ~ trait_off_against_intelligent),
            trait_off_same_open = case_when(binary_offense == "In favour" ~ trait_off_favour_open,
                                            binary_offense == "Against" ~ trait_off_against_open),
            trait_off_same_honest = case_when(binary_offense == "In favour" ~ trait_off_favour_honest,
                                              binary_offense == "Against" ~ trait_off_against_honest),
            trait_off_same_selfish = case_when(binary_offense == "In favour" ~ trait_off_favour_selfish,
                                               binary_offense == "Against" ~ trait_off_against_selfish),
            trait_off_same_hypocritical = case_when(binary_offense == "In favour" ~ trait_off_favour_hypocritical,
                                                    binary_offense == "Against" ~ trait_off_against_hypocritical),
            trait_off_same_closed = case_when(binary_offense == "In favour" ~ trait_off_favour_closed,
                                              binary_offense == "Against" ~ trait_off_against_closed),
            
            trait_off_opposite_intelligent = case_when(binary_offense != "In favour" ~ trait_off_favour_intelligent,
                                                       binary_offense != "Against" ~ trait_off_against_intelligent),
            trait_off_opposite_open = case_when(binary_offense != "In favour" ~ trait_off_favour_open,
                                                binary_offense != "Against" ~ trait_off_against_open),
            trait_off_opposite_honest = case_when(binary_offense != "In favour" ~ trait_off_favour_honest,
                                                  binary_offense != "Against" ~ trait_off_against_honest),
            trait_off_opposite_selfish = case_when(binary_offense != "In favour" ~ trait_off_favour_selfish,
                                                   binary_offense != "Against" ~ trait_off_against_selfish),
            trait_off_opposite_hypocritical = case_when(binary_offense != "In favour" ~ trait_off_favour_hypocritical,
                                                        binary_offense != "Against" ~ trait_off_against_hypocritical),
            trait_off_opposite_closed = case_when(binary_offense != "In favour" ~ trait_off_favour_closed,
                                                  binary_offense != "Against" ~ trait_off_against_closed),
            
            trait_off_scale_same = trait_off_same_intelligent + trait_off_same_open + trait_off_same_honest + (6 - trait_off_same_selfish) + (6 - trait_off_same_hypocritical) + (6 - trait_off_same_closed),
            
            trait_off_scale_same = trait_off_scale_same/6,
            
            trait_off_scale_opposite = trait_off_opposite_intelligent + trait_off_opposite_open + trait_off_opposite_honest + (6 - trait_off_opposite_selfish) + (6 - trait_off_opposite_hypocritical) + (6 - trait_off_opposite_closed),
            
            trait_off_scale_opposite = trait_off_scale_opposite/6,
            
            trait_off_scale = trait_off_scale_same - trait_off_scale_opposite,
            
            ## Thermometer -- i.e. AfeelingN8
            
            ## Changed mind
            
            changed_mind_binary_zero = as_factor(Achanged_mindN1),
            changed_mind_binary_tax = as_factor(Achanged_mindN2),
            changed_mind_binary_mw = as_factor(Achanged_mindN3),
            changed_mind_binary_unemploy = as_factor(Achanged_mindN4),
            changed_mind_binary_trans = as_factor(Achanged_mindN5),
            changed_mind_binary_offense = as_factor(Achanged_mindN6),
            
            changed_mind_binary_zero = factor(changed_mind_binary_zero, levels = c("Against", "In favour")),
            changed_mind_binary_tax = factor(changed_mind_binary_tax, levels = c("Against", "In favour")),
            changed_mind_binary_mw = factor(changed_mind_binary_mw, levels = c("Against", "In favour")),
            changed_mind_binary_unemploy = factor(changed_mind_binary_unemploy, levels = c("Against", "In favour")),
            changed_mind_binary_trans = factor(changed_mind_binary_trans, levels = c("Against", "In favour")),
            changed_mind_binary_offense = factor(changed_mind_binary_offense, levels = c("Against", "In favour")),
            
            binary_zero_2 = changed_mind_binary_zero,
            binary_high_tax_2 = changed_mind_binary_tax,
            binary_mw_2 = changed_mind_binary_mw,
            binary_unemployment_2 = changed_mind_binary_unemploy,
            binary_trans_2 = changed_mind_binary_trans,
            binary_offense_2 = changed_mind_binary_offense,
            
            changed_mind_binary_zero = changed_mind_binary_zero != binary_zero,
            changed_mind_binary_tax = changed_mind_binary_tax != binary_high_tax,
            changed_mind_binary_mw = changed_mind_binary_mw != binary_mw,
            changed_mind_binary_unemploy = changed_mind_binary_unemploy != binary_unemployment,
            changed_mind_binary_trans = changed_mind_binary_trans != binary_trans,
            changed_mind_binary_offense = changed_mind_binary_offense != binary_offense,
            
            ## Happy to discuss
            
            happy_favour_zero = as_factor(AdiscussN1),
            happy_against_zero = as_factor(AdiscussN2),
            happy_favour_high_tax = as_factor(AdiscussN3),
            happy_against_high_tax = as_factor(AdiscussN4),
            happy_favour_mw = as_factor(AdiscussN5),
            happy_against_mw = as_factor(AdiscussN6),
            happy_favour_unemployment = as_factor(AdiscussN7),
            happy_against_unemployment = as_factor(AdiscussN8),
            happy_favour_trans = as_factor(AdiscussN9),
            happy_against_trans = as_factor(AdiscussN10),
            happy_favour_offensive = as_factor(AdiscussN11),
            happy_against_offensive = as_factor(AdiscussN12),
            
            ## Happy to discuss opposite
            
            happy_opposite_zero = case_when(!is.na(happy_favour_zero) ~ happy_favour_zero,
                                            !is.na(happy_against_zero) ~ happy_against_zero),
            
            happy_opposite_high_tax = case_when(!is.na(happy_favour_high_tax) ~ happy_favour_high_tax,
                                            !is.na(happy_against_high_tax) ~ happy_against_high_tax),
            
            happy_opposite_mw = case_when(!is.na(happy_favour_mw) ~ happy_favour_mw,
                                            !is.na(happy_against_mw) ~ happy_against_mw),
            
            happy_opposite_unemployment = case_when(!is.na(happy_favour_unemployment) ~ happy_favour_unemployment,
                                            !is.na(happy_against_unemployment) ~ happy_against_unemployment),
            
            happy_opposite_trans = case_when(!is.na(happy_favour_trans) ~ happy_favour_trans,
                                            !is.na(happy_against_trans) ~ happy_against_trans),
            
            happy_opposite_offensive = case_when(!is.na(happy_favour_offensive) ~ happy_favour_offensive,
                                            !is.na(happy_against_offensive) ~ happy_against_offensive),
            
            
            happy_opposite_zero = factor(happy_opposite_zero, levels = c("Very unhappy", "Fairly unhappy", "Fairly happy", "Very Happy")),
            happy_opposite_high_tax = factor(happy_opposite_high_tax, levels = c("Very unhappy", "Fairly unhappy", "Fairly happy", "Very Happy")),
            happy_opposite_mw = factor(happy_opposite_mw, levels = c("Very unhappy", "Fairly unhappy", "Fairly happy", "Very Happy")),
            happy_opposite_unemployment = factor(happy_opposite_unemployment, levels = c("Very unhappy", "Fairly unhappy", "Fairly happy", "Very Happy")),
            happy_opposite_trans = factor(happy_opposite_trans, levels = c("Very unhappy", "Fairly unhappy", "Fairly happy", "Very Happy")),
            happy_opposite_offensive = factor(happy_opposite_offensive, levels = c("Very unhappy", "Fairly unhappy", "Fairly happy", "Very Happy")),
            
            
            ## Justification rankings
            
            # Tax
            
            just_high_tax_against_treat_1 = 6 - AHigh_tax_against_treat.1,
            just_high_tax_against_treat_2 = 6 - AHigh_tax_against_treat.2,
            just_high_tax_against_treat_3 = 6 - AHigh_tax_against_treat.3,
            just_high_tax_against_treat_4 = 6 - AHigh_tax_against_treat.4,
            just_high_tax_against_treat_5 = 6 - AHigh_tax_against_treat.5,
          
            just_high_tax_against_control_1 = 6 - AHigh_tax_against_control.1,
            just_high_tax_against_control_2 = 6 - AHigh_tax_against_control.2,
            just_high_tax_against_control_3 = 6 - AHigh_tax_against_control.3,
            just_high_tax_against_control_4 = 6 - AHigh_tax_against_control.4,
            just_high_tax_against_control_5 = 6 - AHigh_tax_against_control.5,
            
            just_high_tax_favour_treat_1 = 6 - AHigh_tax_favour_treat.1,
            just_high_tax_favour_treat_2 = 6 - AHigh_tax_favour_treat.2,
            just_high_tax_favour_treat_3 = 6 - AHigh_tax_favour_treat.3,
            just_high_tax_favour_treat_4 = 6 - AHigh_tax_favour_treat.4,
            just_high_tax_favour_treat_5 = 6 - AHigh_tax_favour_treat.5,
            
            just_high_tax_favour_control_1 = 6 - AHigh_tax_favour_control.1,
            just_high_tax_favour_control_2 = 6 - AHigh_tax_favour_control.2,
            just_high_tax_favour_control_3 = 6 - AHigh_tax_favour_control.3,
            just_high_tax_favour_control_4 = 6 - AHigh_tax_favour_control.4,
            just_high_tax_favour_control_5 = 6 - AHigh_tax_favour_control.5,
            
            # Zero
            
            just_zero_against_treat_1 = 6 - Azero_against_treat.1,
            just_zero_against_treat_2 = 6 - Azero_against_treat.2,
            just_zero_against_treat_3 = 6 - Azero_against_treat.3,
            just_zero_against_treat_4 = 6 - Azero_against_treat.4,
            just_zero_against_treat_5 = 6 - Azero_against_treat.5,
            
            just_zero_against_control_1 = 6 - Azero_against_control.1,
            just_zero_against_control_2 = 6 - Azero_against_control.2,
            just_zero_against_control_3 = 6 - Azero_against_control.3,
            just_zero_against_control_4 = 6 - Azero_against_control.4,
            just_zero_against_control_5 = 6 - Azero_against_control.5,
            
            just_zero_favour_treat_1 = 6 - Azero_infavour_treat.1,
            just_zero_favour_treat_2 = 6 - Azero_infavour_treat.2,
            just_zero_favour_treat_3 = 6 - Azero_infavour_treat.3,
            just_zero_favour_treat_4 = 6 - Azero_infavour_treat.4,
            just_zero_favour_treat_5 = 6 - Azero_infavour_treat.5,
            
            just_zero_favour_control_1 = 6 - Azero_infavour_control.1,
            just_zero_favour_control_2 = 6 - Azero_infavour_control.2,
            just_zero_favour_control_3 = 6 - Azero_infavour_control.3,
            just_zero_favour_control_4 = 6 - Azero_infavour_control.4,
            just_zero_favour_control_5 = 6 - Azero_infavour_control.5,
            
            # Wage
            
            just_wage_against_treat_1 = 6 - Awage_against_treat.1,
            just_wage_against_treat_2 = 6 - Awage_against_treat.2,
            just_wage_against_treat_3 = 6 - Awage_against_treat.3,
            just_wage_against_treat_4 = 6 - Awage_against_treat.4,
            just_wage_against_treat_5 = 6 - Awage_against_treat.5,
            
            just_wage_against_control_1 = 6 - Awage_against_control.1,
            just_wage_against_control_2 = 6 - Awage_against_control.2,
            just_wage_against_control_3 = 6 - Awage_against_control.3,
            just_wage_against_control_4 = 6 - Awage_against_control.4,
            just_wage_against_control_5 = 6 - Awage_against_control.5,
            
            just_wage_favour_treat_1 = 6 - Awage_infavour_treat.1,
            just_wage_favour_treat_2 = 6 - Awage_infavour_treat.2,
            just_wage_favour_treat_3 = 6 - Awage_infavour_treat.3,
            just_wage_favour_treat_4 = 6 - Awage_infavour_treat.4,
            just_wage_favour_treat_5 = 6 - Awage_infavour_treat.5,
            
            just_wage_favour_control_1 = 6 - Awage_infavour_control.1,
            just_wage_favour_control_2 = 6 - Awage_infavour_control.2,
            just_wage_favour_control_3 = 6 - Awage_infavour_control.3,
            just_wage_favour_control_4 = 6 - Awage_infavour_control.4,
            just_wage_favour_control_5 = 6 - Awage_infavour_control.5,
            
            # Trans
            
            just_trans_against_treat_1 = 6 - Atrans_against_treat.1,
            just_trans_against_treat_2 = 6 - Atrans_against_treat.2,
            just_trans_against_treat_3 = 6 - Atrans_against_treat.3,
            just_trans_against_treat_4 = 6 - Atrans_against_treat.4,
            just_trans_against_treat_5 = 6 - Atrans_against_treat.5,
            
            just_trans_against_control_1 = 6 - Atrans_against_control.1,
            just_trans_against_control_2 = 6 - Atrans_against_control.2,
            just_trans_against_control_3 = 6 - Atrans_against_control.3,
            just_trans_against_control_4 = 6 - Atrans_against_control.4,
            just_trans_against_control_5 = 6 - Atrans_against_control.5,
            
            just_trans_favour_treat_1 = 6 - Atrans_infavour_treat.1,
            just_trans_favour_treat_2 = 6 - Atrans_infavour_treat.2,
            just_trans_favour_treat_3 = 6 - Atrans_infavour_treat.3,
            just_trans_favour_treat_4 = 6 - Atrans_infavour_treat.4,
            just_trans_favour_treat_5 = 6 - Atrans_infavour_treat.5,
            
            just_trans_favour_control_1 = 6 - Atrans_infavour_control.1,
            just_trans_favour_control_2 = 6 - Atrans_infavour_control.2,
            just_trans_favour_control_3 = 6 - Atrans_infavour_control.3,
            just_trans_favour_control_4 = 6 - Atrans_infavour_control.4,
            just_trans_favour_control_5 = 6 - Atrans_infavour_control.5,
            
            # Unemployment
            
            just_unemployment_against_treat_1 = 6 - Aunemployment_against_treat.1,
            just_unemployment_against_treat_2 = 6 - Aunemployment_against_treat.2,
            just_unemployment_against_treat_3 = 6 - Aunemployment_against_treat.3,
            just_unemployment_against_treat_4 = 6 - Aunemployment_against_treat.4,
            just_unemployment_against_treat_5 = 6 - Aunemployment_against_treat.5,
            
            just_unemployment_against_control_1 = 6 - Aunemployment_against_control.1,
            just_unemployment_against_control_2 = 6 - Aunemployment_against_control.2,
            just_unemployment_against_control_3 = 6 - Aunemployment_against_control.3,
            just_unemployment_against_control_4 = 6 - Aunemployment_against_control.4,
            just_unemployment_against_control_5 = 6 - Aunemployment_against_control.5,
            
            just_unemployment_favour_treat_1 = 6 - Aunemployment_favour_treat.1,
            just_unemployment_favour_treat_2 = 6 - Aunemployment_favour_treat.2,
            just_unemployment_favour_treat_3 = 6 - Aunemployment_favour_treat.3,
            just_unemployment_favour_treat_4 = 6 - Aunemployment_favour_treat.4,
            just_unemployment_favour_treat_5 = 6 - Aunemployment_favour_treat.5,
            
            just_unemployment_favour_control_1 = 6 - Aunemployment_favour_control.1,
            just_unemployment_favour_control_2 = 6 - Aunemployment_favour_control.2,
            just_unemployment_favour_control_3 = 6 - Aunemployment_favour_control.3,
            just_unemployment_favour_control_4 = 6 - Aunemployment_favour_control.4,
            just_unemployment_favour_control_5 = 6 - Aunemployment_favour_control.5,
            
            # Off
            
            just_offensive_against_treat_1 = 6 - Aoffensive_against_treat.1,
            just_offensive_against_treat_2 = 6 - Aoffensive_against_treat.2,
            just_offensive_against_treat_3 = 6 - Aoffensive_against_treat.3,
            just_offensive_against_treat_4 = 6 - Aoffensive_against_treat.4,
            just_offensive_against_treat_5 = 6 - Aoffensive_against_treat.5,
            
            just_offensive_against_control_1 = 6 - Aoffensive_against_control.1,
            just_offensive_against_control_2 = 6 - Aoffensive_against_control.2,
            just_offensive_against_control_3 = 6 - Aoffensive_against_control.3,
            just_offensive_against_control_4 = 6 - Aoffensive_against_control.4,
            just_offensive_against_control_5 = 6 - Aoffensive_against_control.5,
            
            just_offensive_favour_treat_1 = 6 - Aoffensive_infavour_treat.1,
            just_offensive_favour_treat_2 = 6 - Aoffensive_infavour_treat.2,
            just_offensive_favour_treat_3 = 6 - Aoffensive_infavour_treat.3,
            just_offensive_favour_treat_4 = 6 - Aoffensive_infavour_treat.4,
            just_offensive_favour_treat_5 = 6 - Aoffensive_infavour_treat.5,
            
            just_offensive_favour_control_1 = 6 - Aoffensive_infavour_control.1,
            just_offensive_favour_control_2 = 6 - Aoffensive_infavour_control.2,
            just_offensive_favour_control_3 = 6 - Aoffensive_infavour_control.3,
            just_offensive_favour_control_4 = 6 - Aoffensive_infavour_control.4,
            just_offensive_favour_control_5 = 6 - Aoffensive_infavour_control.5,
            
            # Demographics 
            
            vote19 = as_factor(AV005ge19),
            vote16 = as_factor(AV005eu16),
            attention = as_factor(Apol_atten),
            attention = as.numeric(attention),
            
            attention_cat = case_when(attention %in% c(1:3) ~ "Low",
                                      attention %in% c(4:8,12) ~ "Medium",
                                      attention %in% c(9:11) ~ "High"),
            attention_cat = factor(attention_cat, levels = c("Low", "Medium", "High")),
            
            attention_cat2 = case_when(attention %in% c(1:8,12) ~ "Low",
                                       attention %in% c(9:11) ~ "High"),
            attention_cat2 = factor(attention_cat2, levels = c("Low", "High")),
            
            attention = ifelse(attention == 12, NA, attention),
            above_med_attention = attention >=8,
            
            gender = ifelse(CD1_Gender==1, "Male", "Female"),
            age = as.numeric(CD2_Age),
            age_tmp = CD2_Age,
            med_income = APERSONAL_INCOME,
            above_med_income = ifelse(APERSONAL_INCOME < 12, APERSONAL_INCOME, NA) > 2,
            party_simple = case_when(vote19 == "Conservatives" ~ "Conservatives",
                                     vote19 == "Labour" ~ "Labour",
                                     is.na(vote19) ~ NA_character_,
                                     !vote19 %in% c("Labour", "Conservatives") ~ "Other"),
            home_owner = ifelse(ATenure %in% c(1,2),TRUE,FALSE),
            eu_vote = ifelse(grepl("Leave",as.character(as_factor(AV005eu16))), "Leave", "Remain"),
            eu_vote = ifelse(is.na(AV005eu16), "Other", eu_vote),
            eu_vote = factor(eu_vote, levels = c("Leave", "Remain", "Other")),
            social_grade = case_when(AD5_socialgrade %in% c(1,2,3,6) ~ "Working",
                                     AD5_socialgrade %in% c(4,5,7,8) ~ "Not Working"),
            age_cat = case_when(age <= 34 ~ "18-34",
                                age > 34 & age <= 54 ~ "35-54",
                                age > 54 ~ "55+"),
            employment = case_when(AD3_employment == 1 ~ "full_time",
                                   AD3_employment %in% c(2,3) ~ "part_time",
                                   AD3_employment %in% c(4,7) ~ "student_other",
                                   AD3_employment %in% c(5) ~ "retired",
                                   AD3_employment %in% c(6) ~ "unemployed"),
            
            education = case_when(AD_education_1 %in% c(1:5) ~ "Below degree level",
                                  AD_education_1 %in% c(6:11) ~ "Degree level and above"),
            
            region = as_factor(CD4_Region),
            vote19_quota = case_when(AV005ge19 == 1 ~ "Conservative",
                                     AV005ge19 == 2 ~ "Labour",
                                     AV005ge19 == 3 ~ "Liberal Democrats",
                                     AV005ge19 == 4 ~ "Scottish National Party",
                                     AV005ge19 == 5 ~ "Plaid Cymru",
                                     AV005ge19 == 6 ~ "Brexit Party",
                                     AV005ge19 == 8 ~ "Green",
                                     AV005ge19 %in% c(7,8,9) ~ "Other",
                                     is.na(AV005ge19) ~ "Not sure"),
            vote19_quota = factor(vote19_quota, levels = c("Conservative", "Labour", "Liberal Democrats", "Scottish National Party", "Plaid Cymru", "Brexit Party", "Green", "Other", "Not sure")),
            vote19_simple = case_when(AV005ge19 == 1 ~ "Conservative",
                                      AV005ge19 == 2 ~ "Labour",
                                      AV005ge19 %in% c(3:9) ~ "Other",
                                      is.na(AV005ge19) ~ "Other"),
            vote19_simple = factor(vote19_simple, levels = c("Conservative", "Labour", "Other"))
            
            )

# Fix missing match_ids

just_w1[!just_w1$match_id %in% just_w2$match_id,]$match_id <- paste0("w1_missing_w2_", 1:sum(!just_w1$match_id %in% just_w2$match_id))

just_w2[!just_w2$match_id %in% just_w1$match_id,]$match_id <- paste0("w2_missing_w1_", 1:sum(!just_w2$match_id %in% just_w1$match_id))

just_combined <- full_join(just_w1, just_w2, by = "match_id")

just_combined$in_wave_1 <- just_combined$match_id %in% just_w1$match_id
just_combined$in_wave_2 <- just_combined$match_id %in% just_w2$match_id

just_combined$eu_vote.y[just_combined$in_wave_1] <- NA 
just_combined$vote19_quota.y[just_combined$in_wave_1] <- NA

just_w1$in_wave_1 <- TRUE
just_w2$in_wave_2 <- TRUE

just_w1$in_wave_2 <- just_w1$match_id %in% just_w2$match_id
just_w2$in_wave_1 <- just_w2$match_id %in% just_w1$match_id


## Reconcile duplicated variables

just_combined <- just_combined %>% 
  mutate(Id = match_id,
         treat = ifelse(is.na(treat.x), treat.y, treat.x),
         treat = factor(treat, labels = c("Control", "Treatment")),
         
         age = ifelse(is.na(age.x), age.y, age.x),
         age_cat = ifelse(is.na(age_cat.x), age_cat.y, age_cat.x),
         
         gender = case_when(is.na(gender.x) ~ gender.y,
                            !is.na(gender.x) ~ gender.x),
         
         region = case_when(is.na(region.x) ~ region.y,
                            !is.na(region.x) ~ region.x),
         
         social_grade = case_when(is.na(social_grade.x) ~ social_grade.y,
                            !is.na(social_grade.x) ~ social_grade.x),
         
         attention_cat = case_when(is.na(attention_cat.x) ~ attention_cat.y,
                                  !is.na(attention_cat.x) ~ attention_cat.x),
         
         attention_cat2 = case_when(is.na(attention_cat2.x) ~ attention_cat2.y,
                                   !is.na(attention_cat2.x) ~ attention_cat2.x),
         
         attention = case_when(is.na(attention.x) ~ attention.y,
                                   !is.na(attention.x) ~ attention.x),
         
         employment = case_when(is.na(employment.x) ~ employment.y,
                                  !is.na(employment.x) ~ employment.x),
         
         education = case_when(is.na(education.x) ~ education.y,
                                !is.na(education.x) ~ education.x),
         
         vote19_quota = case_when(is.na(vote19_quota.x) ~ vote19_quota.y,
                                !is.na(vote19_quota.x) ~ vote19_quota.x),
         
         vote19_simple = case_when(is.na(vote19_simple.x) ~ vote19_simple.y,
                                   !is.na(vote19_simple.x) ~ vote19_simple.x),
         
         med_income = case_when(is.na(med_income.x) ~ med_income.y,
                                !is.na(med_income.x) ~ med_income.x),
         above_med_income = case_when(is.na(above_med_income.x) ~ above_med_income.y,
                                   !is.na(above_med_income.x) ~ above_med_income.x),
         
         
         eu_vote = case_when(is.na(eu_vote.x) ~ eu_vote.y,
                                  !is.na(eu_vote.x) ~ eu_vote.x),
         
         passed_attention_check_1_w1 = passed_attention_check_1.x,
         passed_attention_check_2_w1 = passed_attention_check_2.x,
         
         passed_attention_check_1_w2 = passed_attention_check_1.y,
         passed_attention_check_2_w2 = passed_attention_check_2.y,
         
         clean = case_when(clean == "Remove" ~ "Remove", 
                           clean == "" ~ "Keep",
                           is.na(clean) ~ "Keep")
         ) %>%
  select(-c(Id.x, Id.y, treat.x, treat.y, age.x, age.y, social_grade.x, social_grade.y, employment.x, employment.y, vote19_quota.x, vote19_quota.y, vote19_simple.x, vote19_simple.y, eu_vote.x, eu_vote.y, age_cat.x, age_cat.y, passed_attention_check_1.x, passed_attention_check_2.x, passed_attention_check_1.y, passed_attention_check_2.y, attention_cat2.x, attention_cat2.y)) %>%
  filter(!is.na(employment)) 

## Remove those failing attention checks (and speeders)

just_w1 <- just_combined %>% filter(passed_attention_check_1_w1 & passed_attention_check_2_w1 & clean == "Keep" & in_wave_1) 

just_w2 <- just_combined %>% filter(passed_attention_check_1_w2 & passed_attention_check_2_w2 & clean == "Keep" & in_wave_2) 

just_panel <- just_combined %>% filter(passed_attention_check_1_w1 & passed_attention_check_2_w1 & clean == "Keep" & in_wave_1 & in_wave_2) 

just_topup <- just_combined %>% filter(passed_attention_check_1_w2 & passed_attention_check_2_w2 & clean == "Keep" & !in_wave_1)

just_w1_topup_combined <- bind_rows(just_w1, just_topup)

just_w1_topup_combined <- just_w1_topup_combined %>%
  mutate(zero_hours = ifelse(is.na(zero_hours), zero_hours_w2, zero_hours),
         minimum_wage = ifelse(is.na(minimum_wage), minimum_wage_w2, minimum_wage),
         unemployment_support = ifelse(is.na(unemployment_support), unemployment_support_w2, unemployment_support),
         high_tax = ifelse(is.na(high_tax), high_tax_w2, high_tax),
         trans_rights = ifelse(is.na(trans_rights), trans_rights_w2, trans_rights),
         offensive_speech = ifelse(is.na(offensive_speech), offensive_speech_w2, offensive_speech))
                                
## Calculate survey weights from Opinium targets

targets <- lapply(1:7, function(x) {
  tmp <- data.frame(readxl::read_excel("../data/survey_targets.xlsx", sheet = x))
  tmp$Freq <- tmp$prop*1000
  if(x==1) {
    tmp[,c(1,2, which(names(tmp)=="Freq"))]
  } else{
    tmp[,c(1, which(names(tmp)=="Freq"))] 
  }
   
  })

get_opinium_weights <- function(my_data = just_w1){
  
  svy <- svydesign(data = my_data, id = ~Id, weights = rep(1, nrow(my_data)))
  
  out <- survey::rake(svy, list(~age_cat +gender, ~region, ~employment, ~social_grade, ~eu_vote, ~vote19_quota, ~attention_cat), targets)
  
  weights(out)
  
  
}

just_w1$weight <- get_opinium_weights(just_w1)
just_w2$weight <- get_opinium_weights(just_w2)
just_panel$weight <- get_opinium_weights(just_panel)
just_topup$weight <- get_opinium_weights(just_topup)
just_w1_topup_combined$weight <- get_opinium_weights(just_w1_topup_combined)

## Add attrition weights

## Within-wave attrition (wave one)

incompletes <- read_sav("../data/incompletes.sav")

incompletes <- incompletes %>%
  transmute(Id = as.character(RespondentID),
            match_id = as.character(RespondentID),
            treat = as_factor(ATreat_Control),
            treat = factor(treat, levels = c("Control", "Treatment")),
            passed_attention_check_1 = ATrap_1 == 1,
            passed_attention_check_2 = ATrap_2 == 5,
            
            survey_date = as.Date(as.character(AEndDate.1), "%Y%m%d"),
            duration = as.numeric(ADuration.1),
            
            vote19 = as_factor(AV005ge19),
            vote16 = as_factor(AV005eu16),
            attention = as_factor(Apol_atten),
            attention = as.numeric(attention),
            
            attention_cat = case_when(attention %in% c(1:3) ~ "Low",
                                      attention %in% c(4:8,12) ~ "Medium",
                                      attention %in% c(9:11) ~ "High"),
            attention_cat = factor(attention_cat, levels = c("Low", "Medium", "High")),
            
            attention_cat2 = case_when(attention %in% c(1:8,12) ~ "Low",
                                       attention %in% c(9:11) ~ "High"),
            attention_cat2 = factor(attention_cat2, levels = c("Low", "High")),
            
            attention = ifelse(attention == 12, NA, attention),
            above_med_attention = attention >=8,
            gender = ifelse(CD1_Gender==1, "Male", "Female"),
            age = as.numeric(CD2_Age),
            med_income = APERSONAL_INCOME,
            above_med_income = ifelse(APERSONAL_INCOME < 12, APERSONAL_INCOME, NA) > 2,
            party_simple = case_when(vote19 == "Conservatives" ~ "Conservatives",
                                     vote19 == "Labour" ~ "Labour",
                                     is.na(vote19) ~ NA_character_,
                                     !vote19 %in% c("Labour", "Conservatives") ~ "Other"),
            home_owner = ifelse(ATenure %in% c(1,2),TRUE,FALSE),
            eu_vote = ifelse(grepl("Leave",as.character(as_factor(AV005eu16))), "Leave", "Remain"),
            eu_vote = ifelse(is.na(AV005eu16), "Other", eu_vote),
            eu_vote = factor(eu_vote, levels = c("Leave", "Remain", "Other")),
            social_grade = case_when(AD5_socialgrade %in% c(1,2,3,6) ~ "Working",
                                     AD5_socialgrade %in% c(4,5,7,8) ~ "Not Working"),
            age_cat = case_when(age <= 34 ~ "18-34",
                                age > 34 & age <= 54 ~ "35-54",
                                age > 54 ~ "55+"),
            employment = case_when(AD3_employment == 1 ~ "full_time",
                                   AD3_employment %in% c(2,3) ~ "part_time",
                                   AD3_employment %in% c(4,7) ~ "student_other",
                                   AD3_employment %in% c(5) ~ "retired",
                                   AD3_employment %in% c(6) ~ "unemployed"),
            
            education = case_when(AD_education_1 %in% c(1:5) ~ "Below degree level",
                                  AD_education_1 %in% c(6:11) ~ "Degree level and above"),
            
            
            region = as_factor(CD4_Region),
            vote19_quota = case_when(AV005ge19 == 1 ~ "Conservative",
                                     AV005ge19 == 2 ~ "Labour",
                                     AV005ge19 == 3 ~ "Liberal Democrats",
                                     AV005ge19 == 4 ~ "Scottish National Party",
                                     AV005ge19 == 5 ~ "Plaid Cymru",
                                     AV005ge19 == 6 ~ "Brexit Party",
                                     AV005ge19 == 8 ~ "Green",
                                     AV005ge19 %in% c(7,8,9) ~ "Other",
                                     is.na(AV005ge19) ~ "Not sure"),
            vote19_quota = factor(vote19_quota, levels = c("Conservative", "Labour", "Liberal Democrats", "Scottish National Party", "Plaid Cymru", "Brexit Party", "Green", "Other", "Not sure")),
            vote19_simple = case_when(AV005ge19 == 1 ~ "Conservative",
                                      AV005ge19 == 2 ~ "Labour",
                                      AV005ge19 %in% c(3:9) ~ "Other",
                                      is.na(AV005ge19) ~ "Other"),
            vote19_simple = factor(vote19_simple, levels = c("Conservative", "Labour", "Other"))
  )

just_w1$incomplete <- FALSE
incompletes$incomplete <- TRUE
both_types <- bind_rows(just_w1, incompletes)


both_types$complete <- !both_types$incomplete

attrition_model <- glm(complete ~ treat * age + 
                         treat * gender + 
                         treat * attention + 
                         treat * employment + 
                         treat * education + 
                         treat * vote19_simple, both_types, family = binomial(link = "logit"))

just_w1$weight_attrition_in_wave <- 1/predict(attrition_model, newdata = just_w1, type = "response")
just_w1$weight_attrition_in_wave[is.na(just_w1$weight_attrition_in_wave)] <- mean(just_w1$weight_attrition_in_wave, na.rm = TRUE)

# in-wave attrition weights for top-up sample (assumes patterns for attrition for new respondents in wave two are the same as the patterns in wave one)

just_topup$weight_attrition_in_wave <- 1/predict(attrition_model, newdata = just_topup, type = "response")
just_topup$weight_attrition_in_wave[is.na(just_topup$weight_attrition_in_wave)] <- mean(just_topup$weight_attrition_in_wave, na.rm = TRUE)

## Panel attrition (wave one and wave two)

attrition_model_panel <- glm(in_wave_2 ~ treat * age + 
                         treat * gender + 
                         treat * attention + 
                         treat * employment + 
                         treat * education + 
                         treat * vote19_simple, just_w1, family = binomial(link = "logit"))

just_panel$weight_attrition_panel <- 1/predict(attrition_model_panel, newdata = just_panel, type = "response")
just_panel$weight_attrition_panel[is.na(just_panel$weight_attrition_panel)] <- mean(just_panel$weight_attrition_panel, na.rm = TRUE)

save(just_w1, just_w2, just_panel, just_topup, just_w1_topup_combined, just_combined, file = "../working/survey.Rdata")

